/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.sigodonto.view.managedbean;

import br.com.sigodonto.model.dao.ItemEstoqueDao;
import br.com.sigodonto.model.entity.ItemEstoque;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import org.primefaces.event.ItemSelectEvent;
import org.primefaces.model.chart.CartesianChartModel;
import org.primefaces.model.chart.ChartSeries;

/**
 *
 * @author Fidelis
 */
@ManagedBean
@ViewScoped
public class AdminClinicaMB implements Serializable{

    /**
     * Creates a new instance of AdminClinicaMB
     */
    
    private CartesianChartModel categoryModel; 
    private ItemEstoqueDao dao;
    private ItemEstoque itemEstoqueSelecionado;
    private List<ItemEstoque> itensEstoque;
    
    public AdminClinicaMB() {
        this.dao = new ItemEstoqueDao();
        categoryModel = new CartesianChartModel();  
  
        this.itensEstoque = this.dao.menorEstoque();
        
        ChartSeries itens = new ChartSeries();  
        itens.setLabel("Item");  
        int i = 0;
        for (ItemEstoque itemEstoque : itensEstoque) {
            if(i == 5)
                break;
            itens.set(itemEstoque.getNome(), itemEstoque.getQuantidade_estoque()); 
            i+=1;
        }
        
        categoryModel.addSeries(itens);  
    }
    
    public void itemSelect(ItemSelectEvent event) {
        int serie = event.getSeriesIndex();
        int indice = event.getItemIndex();
        
        List<ChartSeries> series = this.categoryModel.getSeries();
        
        ChartSeries cs = series.get(serie);
        
        Map<Object, Number> data = cs.getData();
        Set<Object> objs = data.keySet();
        Object[] objects = objs.toArray();
        
        String nome = (String) objects[indice];
        
        for (ItemEstoque itemEstoque : this.itensEstoque) {
            if(itemEstoque.getNome().equals(nome)){
                this.itemEstoqueSelecionado = itemEstoque;
            }
        }
        
    }

    public CartesianChartModel getCategoryModel() {
        return categoryModel;
    }

    public void setCategoryModel(CartesianChartModel categoryModel) {
        this.categoryModel = categoryModel;
    }

    public ItemEstoque getItemEstoqueSelecionado() {
        return itemEstoqueSelecionado;
    }

    public void setItemEstoqueSelecionado(ItemEstoque itemEstoqueSelecionado) {
        this.itemEstoqueSelecionado = itemEstoqueSelecionado;
    }
    
    
    
    
}
